﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BitrateCalc
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private double time;

        #region Helper Functions
        private double Kbit2Byte(int Kbit)
        {
            double res = (double)Kbit / 8;
            res *= 1024;
            return res;
        }

        private string DynamicFilesize(double fsize)
        {
            //Gb -> 1073741824 byte
            //Mb ->    1048576 byte
            //Kb ->       1024 byte
            double val = fsize;
            if (fsize > 1073741824)
            {
                val /= 1073741824;
                return Math.Round(val, 3).ToString() + " Gb";
            }
            else if (fsize > 1048576)
            {
                val /= 1048576;
                return Math.Round(val, 3).ToString() + " Mb";
            }
            else if (fsize > 1024)
            {
                val /= 1024;
                return Math.Round(val, 3).ToString() + " Kb";
            }
            else return val.ToString() + " bytes";
        }

        private void ParseTime()
        {
            double tmp;
            this.time = 0;
            if (THms.Checked)
            {
                string[] s = InLength.Text.Split(':');
                tmp = Convert.ToDouble(s[0]);
                this.time += tmp * 3600;
                tmp = Convert.ToDouble(s[1]);
                this.time += tmp * 60;
                tmp = Convert.ToDouble(s[2]);
                this.time += tmp;
            }
            else if (TH.Checked)
            {
                tmp = Convert.ToDouble(InLength.Text);
                this.time += tmp * 3600;
            }
            else if (TM.Checked)
            {
                tmp = Convert.ToDouble(InLength.Text);
                this.time += tmp * 60;
            }
            else this.time = Convert.ToDouble(InLength.Text);
        }

        #endregion

        #region Calculations

        private double CalcFileSize(int Abr, int Vbr, double Time)
        {
            double res = 0;
            res += Time;
            res *= Kbit2Byte(Abr);
            res *= Kbit2Byte(Vbr);
            return res;
        }

        #endregion

        private void calculateToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void TimeInputChange(object sender, EventArgs e)
        {
            RadioButton s = (RadioButton)sender;
            switch (s.Name)
            {
                case "THms":
                    this.ParseTime();
                    this.InLength.Mask = "00:00:00";
                    this.InLength.Text = this.time.ToString();
                    break;
                case "TH":
                    this.ParseTime();
                    this.InLength.Mask = "";
                    this.InLength.Text = this.time.ToString();
                    break;
                case "TM":
                    this.ParseTime();
                    this.InLength.Mask = "";
                    this.InLength.Text = this.time.ToString();
                    break;
                case "TS":
                    this.ParseTime();
                    this.InLength.Mask = "";
                    this.InLength.Text = this.time.ToString();
                    break;
            }
        }

        private void CalcTypeChange(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem it in calculationTypeToolStripMenuItem.DropDown.Items) it.Checked = false;
            ((ToolStripMenuItem)sender).Checked = true;
        }
    }
}
